package br.com.duduso.conferencia_claro_desktop.screens;

import br.com.duduso.conferencia_claro_desktop.components.FrameInterno;
import br.com.duduso.conferencia_claro_desktop.components.BarraProgresso;
import br.com.duduso.conferencia_claro_desktop.components.Mensagens;
import br.com.duduso.conferencia_claro_desktop.enums.EnvioEmail;
import br.com.duduso.conferencia_claro_desktop.model.CustomTableModel;
import br.com.duduso.conferencia_claro_desktop.services.ConferenciasService;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author duduso
 */
public class TelaDetalhesContatos extends FrameInterno {

    private ConferenciasService service;
    private List<File> arquivosConferencia;

    public TelaDetalhesContatos(File arquivoTotais, ConferenciasService service, 
            List<File> arquivosConferencia) {
            super();
            this.service = service;
            this.arquivosConferencia = arquivosConferencia;
            initComponents();
            popularTabelaClientes(arquivoTotais, service);
            popularComboEnvioEmails();
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnContatos = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaClientes = new javax.swing.JTable();
        btFechar = new javax.swing.JButton();
        btDetalharEnviar = new javax.swing.JButton();
        lbEnvioEmails = new javax.swing.JLabel();
        comboEnvioEmails = new javax.swing.JComboBox();

        setBackground(new java.awt.Color(193, 205, 205));
        setBorder(null);
        setForeground(new java.awt.Color(255, 255, 255));
        setOpaque(false);

        pnContatos.setBackground(new java.awt.Color(193, 205, 205));
        pnContatos.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "CLIENTES", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 1, 12))); // NOI18N
        pnContatos.setName("pnContatos"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabelaClientes.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        tabelaClientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tabelaClientes.setName("tabelaClientes"); // NOI18N
        tabelaClientes.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tabelaClientes);

        btFechar.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        btFechar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/close.png"))); // NOI18N
        btFechar.setText("<html><b>Fechar</b></html>");
        btFechar.setName("btFechar"); // NOI18N
        btFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btFecharActionPerformed(evt);
            }
        });

        btDetalharEnviar.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        btDetalharEnviar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/detail.png"))); // NOI18N
        btDetalharEnviar.setText("<html><b>Detalhar/Enviar</b></html>");
        btDetalharEnviar.setName("btDetalharEnviar"); // NOI18N
        btDetalharEnviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDetalharEnviarActionPerformed(evt);
            }
        });

        lbEnvioEmails.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        lbEnvioEmails.setText("<html><b>Opções de Envio: </b></html>");
        lbEnvioEmails.setName("lbEnvioEmails"); // NOI18N

        comboEnvioEmails.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        comboEnvioEmails.setName("comboEnvioEmails"); // NOI18N

        javax.swing.GroupLayout pnContatosLayout = new javax.swing.GroupLayout(pnContatos);
        pnContatos.setLayout(pnContatosLayout);
        pnContatosLayout.setHorizontalGroup(
            pnContatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnContatosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnContatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnContatosLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 898, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(pnContatosLayout.createSequentialGroup()
                        .addComponent(lbEnvioEmails, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboEnvioEmails, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(470, 470, 470))
                    .addGroup(pnContatosLayout.createSequentialGroup()
                        .addComponent(btDetalharEnviar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btFechar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(715, Short.MAX_VALUE))))
        );
        pnContatosLayout.setVerticalGroup(
            pnContatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnContatosLayout.createSequentialGroup()
                .addGroup(pnContatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbEnvioEmails, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboEnvioEmails, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 393, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnContatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btFechar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btDetalharEnviar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 956, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnContatos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 534, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnContatos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btFecharActionPerformed
        fecharTab();
}//GEN-LAST:event_btFecharActionPerformed

    private void btDetalharEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDetalharEnviarActionPerformed
        new BarraProgresso() {
                @Override
                public void executar() {
                        try {
                                service.detalharEnviar(getLinhasSelecionadas(), arquivosConferencia,
                                        String.valueOf(comboEnvioEmails.getSelectedItem()));
                                Mensagens.getInstance().exibirMensagemInformacao(null, "msgDetalhesEnviadosSucesso");
                        } catch (Exception ex) {
                                JOptionPane.showMessageDialog(null, ex);
                        }
                }
        };
}//GEN-LAST:event_btDetalharEnviarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btDetalharEnviar;
    private javax.swing.JButton btFechar;
    private javax.swing.JComboBox comboEnvioEmails;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbEnvioEmails;
    private javax.swing.JPanel pnContatos;
    private javax.swing.JTable tabelaClientes;
    // End of variables declaration//GEN-END:variables

    private void popularTabelaClientes(File file, ConferenciasService conferenciasService){
            try {
                    List<Object[]> clientes = conferenciasService.obterListaClientes(file);
                    CustomTableModel ctm = new CustomTableModel();
                    ctm.addColumn(Mensagens.getInstance().getMessage("contatosClientes"));
                    ctm.addColumn(Mensagens.getInstance().getMessage("contatosTelefones"));
                    ctm.addColumn(Mensagens.getInstance().getMessage("contatosEmail"));
                    ctm.addColumn(Mensagens.getInstance().getMessage("contatosDesconto"));
                    for(Object[] cliente : clientes){
                            ctm.addRow(cliente);
                    }
                    this.tabelaClientes.setModel(ctm);
            } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, ex);
            }
    }

    private List<Object[]> getLinhasSelecionadas(){
            int[] linhasSelecionadas = tabelaClientes.getSelectedRows();
            List<Object[]> values = new ArrayList<Object[]>();
            for(int i = 0; i < linhasSelecionadas.length; i++){
                    int selectedRow = linhasSelecionadas[i];
                    Object[] value = new Object[4];
                    //Cliente
                    value[0] = tabelaClientes.getModel().getValueAt(selectedRow, 0);
                    //Número do Telefone
                    value[1] = tabelaClientes.getModel().getValueAt(selectedRow, 1);
                    //E-mail
                    value[2] = tabelaClientes.getModel().getValueAt(selectedRow, 2);
                    //Desconto
                    value[3] = tabelaClientes.getModel().getValueAt(selectedRow, 3);
                    values.add(value);
            }
            return values;
    }

    @Override
    public void dispose(){
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            super.dispose();
    }

    private void popularComboEnvioEmails(){
            this.comboEnvioEmails.addItem(EnvioEmail.MEU_EMAIL.getValue());
            this.comboEnvioEmails.addItem(EnvioEmail.EMAIL_COLABORADOR.getValue());
    }

}
